package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@Slf4j
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Override
    public void ordersAndorderDtail(Orders orders) {
        //获取用户的id
        Long currentId = BaseContext.getCurrentId();
        //设置用户的id 线程的局部变量中
        orders.setUserId(currentId);
        //获取用户名
        User byId = userService.getById(currentId);
        //获取用户的手机号
        orders.setPhone(byId.getPhone());
        //通过地址id 获取地址信息
        Long addressBookId = orders.getAddressBookId();
        AddressBook byId1 = addressBookService.getById(addressBookId);
        orders.setAddress(byId1.getDetail());
        //添加收货人
        orders.setConsignee(byId1.getConsignee());
        //添加下单时间
        orders.setOrderTime(LocalDateTime.now());
        //添加结账时间
        orders.setCheckoutTime(LocalDateTime.now());
        //设置订单号 id
        long id = IdWorker.getId();
        orders.setNumber(id+"");
        //通过用户的id获取改用户购物车的所有购物项
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //条件 用户的id
        wrapper.eq(ShoppingCart::getUserId,currentId);
        //改用户的所有购物项
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        //创建一个集合 放 订单详情信息  订单详情里放的是改用户的购物项
        List<OrderDetail> list1 = new ArrayList<>();
        //
        AtomicInteger atomicInteger = new AtomicInteger(0);//累计变量
        //遍历所有购物项集合
        for (final ShoppingCart shoppingCart : list) {
            //创建订单详情
            OrderDetail orderDetail = new OrderDetail();
            //将购物项对象 copy到订单详情对象里
            BeanUtils.copyProperties(shoppingCart,orderDetail,"orderId");
            //加入订单的id
            orderDetail.setOrderId(id);
            list1.add(orderDetail);
            //yige ca 的单价
            BigDecimal danjia = orderDetail.getAmount();
            //省略后两位  分
            int number = orderDetail.getNumber();
            BigDecimal geshu = new BigDecimal(number + "");
            //省略后两位分
            atomicInteger.addAndGet((danjia.multiply(geshu)).intValue());
        }
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        this.save(orders);
        orderDetailService.saveBatch(list1);

        shoppingCartService.remove(wrapper);
    }
}
